﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace FSquirrel.Helpers
{
    [ValueConversion(typeof(int), typeof(string))]
    public class FileSizeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && (int)value > 0.0)
            {
                int bytes = (int)value;
                string[] suf = { "MB", "GB", "TB", "PB" };
                int place = Int32.Parse((Math.Floor(Math.Log(bytes, 1024))).ToString());
                double num = Math.Round(bytes / Math.Pow(1024, place), 1);
                return string.Format("{0} {1}", num, suf[place]);
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //not implemented for now
            throw new NotImplementedException();
        }
    }
}